﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard;
using Orchard.Localization;
using Orchard.DisplayManagement;
using System.IO;
using System.Web.Mvc;
using Orchard.Environment;
using Orchard.UI.Resources;

namespace EMS.Calendar.Providers.Layouts {
    public class LayoutShapes : IDependency {
        private readonly Work<IResourceManager> _resourceManager;

        public LayoutShapes(Work<IResourceManager> resourceManager) {
            _resourceManager = resourceManager;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        [Shape]
        public void Calendar(dynamic Display, TextWriter Output, HtmlHelper Html, string Id, IEnumerable<dynamic> Items, IEnumerable<string> Classes, IDictionary<string, string> Attributes) {
            if (Items == null)
                return;

            var items = Items.ToList();
            var itemsCount = items.Count;

            if (itemsCount < 1)
                return;
                        
            // Add in stylesheets
            _resourceManager.Value.Require("stylesheet", "Calendar");
            _resourceManager.Value.Require("stylesheet", "CalendarPrint");
            _resourceManager.Value.Require("stylesheet", "CalendarTheme");

            // Add in scripts
            _resourceManager.Value.Require("script", "Calendar").AtHead();

            // build up javascript that renders the items
            Output.WriteLine("<script type='text/javascript'>");
            Output.WriteLine("  $(document).ready(function() {");
            Output.WriteLine(String.Format("      var calendar = $('#{0}').fullCalendar({", Id));
            Output.WriteLine("          header: {");
            Output.WriteLine("              left: 'prev,next today',");
            Output.WriteLine("              center: 'title',");
            Output.WriteLine("              right: 'month,agendaWeek,agendaDay'");
            Output.WriteLine("          },");
            Output.WriteLine("          editable: false,");
            Output.WriteLine("          events: [");

            foreach (var item in items) {
                bool allDay = false;
                string title = "All Day Event";
                string url = "http://www.google.com/";
                DateTime start = DateTime.Now;
                DateTime end = DateTime.Now.Add(new TimeSpan(0,1,0,0,0));

                Output.WriteLine("      {");
                Output.WriteLine(String.Format("            allDay: {0}", allDay));
                Output.WriteLine(String.Format("            title: {0}", title));
                Output.WriteLine(String.Format("            url: {0}", url));
                Output.WriteLine(String.Format("            start: {0}", start));
                Output.WriteLine(String.Format("            end: {0}", end));
                Output.WriteLine("      },"); // May need to remove last comma
            }
            Output.WriteLine("          ]");
            Output.WriteLine("      });");
            Output.WriteLine("  });");
            Output.WriteLine("</script>");
		    
            // Add in div (with same id as above) to render the calendar into
            Output.WriteLine(String.Format("<div id='{0}'></div>", Id));        
        }
    }
}